View Javadoc

1   /*
2   * Copyright 2005 Arnaud Prost
3   * 
4   * Arnaud.prost@gmail.com
5   * 
6   * This software is a computer program whose purpose is to ease the 
7   * management of software project.
8   * 
9   * This software is governed by the CeCILL  license under French law and
10  * abiding by the rules of distribution of free software.  You can  use, 
11  * modify and/ or redistribute the software under the terms of the CeCILL
12  * license as circulated by CEA, CNRS and INRIA at the following URL
13  * "http://www.cecill.info". 
14  * 
15  * As a counterpart to the access to the source code and  rights to copy,
16  * modify and redistribute granted by the license, users are provided only
17  * with a limited warranty  and the software's author,  the holder of the
18  * economic rights,  and the successive licensors  have only  limited
19  * liability. 
20  * 
21  * In this respect, the user's attention is drawn to the risks associated
22  * with loading,  using,  modifying and/or developing or reproducing the
23  * software by the user in light of its specific status of free software,
24  * that may mean  that it is complicated to manipulate,  and  that  also
25  * therefore means  that it is reserved for developers  and  experienced
26  * professionals having in-depth computer knowledge. Users are therefore
27  * encouraged to load and test the software's suitability as regards their
28  * requirements in conditions enabling the security of their systems and/or 
29  * data to be ensured and,  more generally, to use and operate it in the 
30  * same conditions as regards security. 
31  * 
32  * The fact that you are presently reading this means that you have had
33  * knowledge of the CeCILL license and that you accept its terms.
34  */
35  package net.sf.pmr.agilePlanning.service;
36  
37  import java.util.Iterator;
38  import java.util.Set;
39  
40  import net.sf.pmr.agilePlanning.AgilePlanningObjectFactory;
41  import net.sf.pmr.agilePlanning.domain.iteration.Iteration;
42  import net.sf.pmr.agilePlanning.domain.story.Story;
43  import net.sf.pmr.agilePlanning.domain.story.StoryRepository;
44  import net.sf.pmr.agilePlanning.domain.story.task.Task;
45  import net.sf.pmr.agilePlanning.domain.story.task.TaskValidator;
46  import net.sf.pmr.core.CoreObjectFactory;
47  import net.sf.pmr.core.domain.user.User;
48  import net.sf.pmr.keopsframework.domain.validation.Errors;
49  
50  /***
51   * @author Arnaud Prost (arnaud.prost@gmail.com)
52   */
53  public class StoryServiceImpl implements StoryService {
54  
55      /***
56       * Validator
57       */
58      private TaskValidator taskValidator;
59  
60      /***
61       * Repository
62       */
63      private StoryRepository storyRepository;
64  
65      public StoryServiceImpl(final TaskValidator taskValidator,
66              final StoryRepository storyRepository) {
67          super();
68          this.taskValidator = taskValidator;
69          this.storyRepository = storyRepository;
70      }
71  
72      /***
73       * @see net.sf.pmr.agilePlanning.service.StoryService#add(int, java.lang.String, java.lang.String, int)
74       */
75      public Errors add(final int projectPersistanceId,
76              final String shortDescription, final String description,
77              final int estimate, final int iterationPersistanceId) {
78  
79          // Build the object to persist
80          Story story = AgilePlanningObjectFactory.getStory();
81          story.getBasicProject().setPersistanceId(projectPersistanceId);
82          story.setDescription(description);
83          
84          Iteration iteration = AgilePlanningObjectFactory.getIteration();
85          iteration.setPersistanceId(iterationPersistanceId);
86  
87          story.setIteration(iteration);
88          
89          story.setEstimate(estimate);
90          story.setShortDescription(shortDescription);
91  
92          //persist
93          storyRepository.addOrUpdate(story);
94  
95          // aucune erreur retournée
96          return AgilePlanningObjectFactory.getErrors();
97  
98      }
99  
100     /***
101      * @see net.sf.pmr.agilePlanning.service.StoryService#addTask(int, int,
102      *      java.lang.String, int)
103      */
104     public Errors addTask(final int storyPersistanceId,
105             final int numberOfIdealDays, final String shortDescription,
106             final int developperPersistanceId) {
107 
108         // Build the object to persist
109         Task task = AgilePlanningObjectFactory.getTask();
110         User user = CoreObjectFactory.getUser();
111         user.setPersistanceId(developperPersistanceId);
112 
113         task.setDevelopper(user);
114         task.setIdealDay(numberOfIdealDays);
115         task.setShortDescription(shortDescription);
116 
117         // validate
118         Errors errors = taskValidator.validate(task);
119 
120         if (!errors.hasErrors()) {
121 
122             // find the story
123             Story story = storyRepository
124                     .findByPersistanceId(storyPersistanceId);
125 
126             // add the task
127             if (story != null) {
128 
129                 story.getTasks().add(task);
130 
131                 storyRepository.addOrUpdate(story);
132 
133             }
134 
135         }
136 
137         return errors;
138 
139     }
140 
141     //    /***
142     //     * @see net.sf.pmr.agilePlanning.service.StoryService#delete(int, long)
143     //     */
144     //    public Errors delete(int persistanceId, long persistanceVersion) {
145     //        return null;
146     //    }
147 
148     /***
149      * @see net.sf.pmr.agilePlanning.service.StoryService#findByPersistanceId(int)
150      */
151     public Story findByPersistanceId(final int persistanceId) {
152         return storyRepository.findByPersistanceId(persistanceId);
153     }
154 
155     /***
156      * @see net.sf.pmr.agilePlanning.service.StoryService#findByProjectPersistanceId(int)
157      */
158     public Set findByProjectPersistanceId(final int projectPersistanceId) {
159         return storyRepository.findByProjectPersistanceId(projectPersistanceId);
160     }
161 
162     /***
163      * @see net.sf.pmr.agilePlanning.service.StoryService#findTaskByPersistanceId(int)
164      */
165     public Task findTaskByPersistanceId(final int persistanceId) {
166         return storyRepository.findTaskByPersistanceId(persistanceId);
167     }
168 
169     /***
170      * @see net.sf.pmr.agilePlanning.service.StoryService#update(int, java.lang.String, java.lang.String, int, int, int, long)
171      */
172     public Errors update(final int projectPersistanceId,
173             final String shortDescription, final String description,
174             final int estimate, final int iterationPersistanceId, final int persistanceId,
175             final long persistanceVersion) {
176 
177         // Build the object to persist
178         Story story = AgilePlanningObjectFactory.getStory();
179         story.getBasicProject().setPersistanceId(projectPersistanceId);
180         story.setDescription(description);
181         story.setEstimate(estimate);
182 
183         Iteration iteration = AgilePlanningObjectFactory.getIteration();
184         iteration.setPersistanceId(iterationPersistanceId);
185 
186         story.setIteration(iteration);
187         story.setPersistanceId(persistanceId);
188         story.setPersistanceVersion(persistanceVersion);
189         story.setShortDescription(shortDescription);
190 
191         // persist
192         storyRepository.addOrUpdate(story);
193 
194         // aucune erreur retournée
195         return AgilePlanningObjectFactory.getErrors();
196 
197     }
198 
199     /***
200      * @see net.sf.pmr.agilePlanning.service.StoryService#updateTask(int, int, java.lang.String, int, int, long)
201      */
202     public Errors updateTask(final int storyPersistanceId,
203             final int numberOfIdealDays, final String shortDescription,
204             final int developperPersistanceId, final int persistanceId,
205             final long persistanceVersion) {
206 
207         // Build the object to persist
208         Task task = AgilePlanningObjectFactory.getTask();
209         User user = CoreObjectFactory.getUser();
210         user.setPersistanceId(developperPersistanceId);
211 
212         task.setDevelopper(user);
213         task.setPersistanceId(persistanceId);
214         task.setPersistanceVersion(persistanceVersion);
215         task.setIdealDay(numberOfIdealDays);
216         task.setShortDescription(shortDescription);
217 
218         // validate
219         Errors errors = taskValidator.validate(task);
220 
221         if (!errors.hasErrors()) {
222 
223             // find the story
224             Story story = storyRepository
225                     .findByPersistanceId(storyPersistanceId);
226 
227             if (story != null) {
228 
229                 // find the task to update
230                 for (Iterator iterator = story.getTasks().iterator(); iterator
231                         .hasNext();) {
232                     Task taskToUpdate = (Task) iterator.next();
233 
234                     if (task.getPersistanceId() == taskToUpdate
235                             .getPersistanceId()) {
236 
237                         taskToUpdate.setDevelopper(task.getDevelopper());
238                         taskToUpdate.setIdealDay(task.getIdealDay());
239                         taskToUpdate.setPersistanceVersion(task
240                                 .getPersistanceVersion());
241                         taskToUpdate.setShortDescription(task
242                                 .getShortDescription());
243 
244                         // persist
245                         storyRepository.addOrUpdate(story);
246 
247                         // finish
248                         break;
249 
250                     }
251 
252                 }
253 
254             }
255 
256         }
257 
258         return errors;
259 
260     }
261 }